package com.hq.fxh.No1_Infromation.Infor.Adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hq.fxh.R;
import com.hq.fxh.Utils.DensityUtils;
import com.hq.fxh.Utils.SystemUtils;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created ${data}
 * @Email xlwenhao1858@sina.com
 */

public class Adapter_recycle_infor extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mcontext;
    ArrayList<String> data;
    Help_Infor_NewsType1 newsType1;
    Help_Infor_NewsType2 newsType2;
    Help_Infor_NewsType3 newsType3;
    Help_Infor_NewsType4 newsType4;
    RecyclerView recyclerView;

    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }

    public Adapter_recycle_infor(Context context, ArrayList list) {
        mcontext = context;
        data = list;
    }

    public void setData(ArrayList<String> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewByViewType = getViewByViewType(viewType);
        return viewByViewType;
    }

    private RecyclerView.ViewHolder getViewByViewType(int type) {
        RecyclerView.ViewHolder viewHolder = null;
        View topBannerViewHolder = View.inflate(mcontext, R.layout.xbanner, null);//轮播图
        View gridviewViewHolder = View.inflate(mcontext, R.layout.gridview, null);//
        View newType1ViewHolder = View.inflate(mcontext, R.layout.adapter_infor_news1, null);//
        View newType2ViewHolder = View.inflate(mcontext, R.layout.adapter_infor_news2, null);//
        View newType3ViewHolder = View.inflate(mcontext, R.layout.adapter_infor_news3, null);//
        View newType4ViewHolder = View.inflate(mcontext, R.layout.adapter_infor_news4, null);//

        if (type == 1) {
            viewHolder = new TopBannerViewHolder(topBannerViewHolder);
        } else if (type == 2) {
            viewHolder = new GridViewHolder(gridviewViewHolder);
        } else if (type == 3) {
            viewHolder = new NewsType1ViewHolder(newType1ViewHolder);
        } else if (type == 4) {
            viewHolder = new NewsType2ViewHolder(newType2ViewHolder);
        } else if (type == 5) {
            viewHolder = new NewsType3ViewHolder(newType3ViewHolder);
        } else if (type == 6) {
            viewHolder = new NewsType4ViewHolder(newType4ViewHolder);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemViewType = holder.getItemViewType();

        if (itemViewType == 1) {
            TopBannerViewHolder mholder = (TopBannerViewHolder) holder;
            new Help_Infor_Banner(mcontext, mholder, data).doForBanner();
        } else if (itemViewType == 2) {
            GridViewHolder mholder = (GridViewHolder) holder;
            //    Help_Infor_GridView help_infor_gridView = new Help_Infor_GridView(mcontext, mholder);
            //     help_infor_gridView.doForGridView();
        } else if (itemViewType == 3) {
            NewsType1ViewHolder mholder = (NewsType1ViewHolder) holder;
            newsType1 = new Help_Infor_NewsType1(mcontext, mholder);
            newsType1.setRecyclerView(recyclerView);
            if (position == 2) {
                newsType1.doForNewsType1(1);
            }
            if (position == 3) {
                newsType1.doForNewsType1(2);
            }
            if (position == 4) {
                newsType1.doForNewsType1(3);
            }
        } else if (itemViewType == 4) {
            NewsType2ViewHolder mholder = (NewsType2ViewHolder) holder;
            newsType2 = new Help_Infor_NewsType2(mcontext, mholder);
            if (position == 5) {
                newsType2.doForNewsType2(1);
            }
            if (position == 6) {
                newsType2.doForNewsType2(2);
            }
            if (position == 7) {
                newsType2.doForNewsType2(3);
            }


        } else if (itemViewType == 5) {
            NewsType3ViewHolder mholder = (NewsType3ViewHolder) holder;
            newsType3 = new Help_Infor_NewsType3(mcontext, mholder);
            if (position == 8) {
                newsType3.doForNewsType3(1);
            }
            if (position == 9) {
                newsType3.doForNewsType3(2);
            }
            if (position == 10) {
                newsType3.doForNewsType3(3);
            }
        } else if (itemViewType == 6) {
            NewsType4ViewHolder mholder = (NewsType4ViewHolder) holder;
            newsType4 = new Help_Infor_NewsType4(mcontext, mholder);
            if (position == 11) {
                newsType4.doForNewsType4(1);
            }
            if (position == 12) {
                newsType4.doForNewsType4(2);
            }
            if (position == 13) {
                newsType4.doForNewsType4(3);
            }
        }

    }


    @Override
    public int getItemCount() {
        return 14;
    }

    @Override
    public int getItemViewType(int position) {
        int type = -1;
        if (position == 0) {
            type = 1;
        } else if (position == 1) {
            type = 2;
        } else if (position == 2 || position == 3 || position == 4) {
            type = 3;
        } else if (position == 5 || position == 6 || position == 7) {
            type = 4;
        } else if (position == 8 || position == 9 || position == 10) {
            type = 5;
        } else if (position == 11 || position == 12 || position == 13) {
            type = 6;
        }
        return type;
    }


    public class TopBannerViewHolder extends RecyclerView.ViewHolder {
        XBanner xBanner;

        public TopBannerViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View itemView) {
            xBanner = (XBanner) itemView.findViewById(R.id.xbanner);
        }
    }

    public class GridViewHolder extends RecyclerView.ViewHolder {
        GridView gridView;

        public GridViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View itemView) {
            gridView = (GridView) itemView.findViewById(R.id.item_gridview);
        }
    }

    public class NewsType1ViewHolder extends RecyclerView.ViewHolder {

        protected TextView textTitle;
        protected LinearLayout newsType1LinyoutAD;
        protected ImageView authorIcon;
        protected TextView authorName;
        protected TextView newsTag;
        protected LinearLayout NewsType1LinyoutAuthor;
        protected TextView copyRightName;
        protected LinearLayout NewsType1LinyoutCopyRight;
        protected TextView readNewNum;
        protected LinearLayout newsType1LinyoutNoAD;
        protected LinearLayout newsType1;

        protected ImageView newImageView;
        protected LinearLayout news_author_tag;
        protected LinearLayout pingbi;

        public NewsType1ViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View rootView) {
            textTitle = (TextView) rootView.findViewById(R.id.text_title);
            newsType1LinyoutAD = (LinearLayout) rootView.findViewById(R.id.newsType1_Linyout_AD);
            authorIcon = (ImageView) rootView.findViewById(R.id.author_icon);
            authorName = (TextView) rootView.findViewById(R.id.author_name);
            newsTag = (TextView) rootView.findViewById(R.id.news_tag);
            NewsType1LinyoutAuthor = (LinearLayout) rootView.findViewById(R.id.NewsType1_Linyout_author);
            copyRightName = (TextView) rootView.findViewById(R.id.copyRight_name);
            NewsType1LinyoutCopyRight = (LinearLayout) rootView.findViewById(R.id.NewsType1_Linyout_CopyRight);
            readNewNum = (TextView) rootView.findViewById(R.id.readNewNum);
            newsType1LinyoutNoAD = (LinearLayout) rootView.findViewById(R.id.newsType1_Linyout_NoAD);
            newsType1 = (LinearLayout) rootView.findViewById(R.id.newsType1);
            news_author_tag = (LinearLayout) rootView.findViewById(R.id.news_author_tag);
            pingbi = (LinearLayout) rootView.findViewById(R.id.pingbi);
            newImageView = (ImageView) rootView.findViewById(R.id.new_imageView);
        }


    }

    public class NewsType2ViewHolder extends RecyclerView.ViewHolder {
        protected TextView textTitle;
        protected LinearLayout newsType2LinyoutAD;
        protected ImageView authorIcon;
        protected ImageView newsImage1;
        protected ImageView newsImage2;
        protected ImageView newsImage3;
        protected TextView authorName;
        protected TextView newsTag;
        protected LinearLayout NewsType2LinyoutAuthor;
        protected TextView copyRightName;
        protected LinearLayout NewsType2LinyoutCopyRight;
        protected TextView readNewNum;
        protected LinearLayout newsType2LinyoutNoAD;
        protected LinearLayout newsType2;
        protected LinearLayout news_author_tag;
        protected LinearLayout pingbi;

        public NewsType2ViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View rootView) {
            newsType2 = (LinearLayout) rootView.findViewById(R.id.newsType2);

            textTitle = (TextView) rootView.findViewById(R.id.text_title);
            newsType2LinyoutAD = (LinearLayout) rootView.findViewById(R.id.newsType2_Linyout_AD);
            authorIcon = (ImageView) rootView.findViewById(R.id.author_icon);
            authorName = (TextView) rootView.findViewById(R.id.author_name);
            newsTag = (TextView) rootView.findViewById(R.id.news_tag);
            NewsType2LinyoutAuthor = (LinearLayout) rootView.findViewById(R.id.NewsType2_Linyout_author);
            copyRightName = (TextView) rootView.findViewById(R.id.copyRight_name);
            NewsType2LinyoutCopyRight = (LinearLayout) rootView.findViewById(R.id.NewsType2_Linyout_CopyRight);
            readNewNum = (TextView) rootView.findViewById(R.id.readNewNum);
            newsType2LinyoutNoAD = (LinearLayout) rootView.findViewById(R.id.newsType2_Linyout_NoAD);
            newsImage1 = (ImageView) rootView.findViewById(R.id.news_image1);
            newsImage2 = (ImageView) rootView.findViewById(R.id.news_image2);
            newsImage3 = (ImageView) rootView.findViewById(R.id.news_image3);
            news_author_tag = (LinearLayout) rootView.findViewById(R.id.news_author_tag);
            pingbi = (LinearLayout) rootView.findViewById(R.id.pingbi);
            float windowWidth = (float) SystemUtils.getWindowWidth((Activity) mcontext);
            int i = DensityUtils.dip2px(mcontext, 40);
            int viewWidth = (int) (windowWidth - i) / 3;
            setImageWH(newsImage1, viewWidth, (viewWidth / 4) * 3);
            setImageWH(newsImage2, viewWidth, (viewWidth / 4) * 3);
            setImageWH(newsImage3, viewWidth, (viewWidth / 4) * 3);
        }
    }

    public void setImageWH(ImageView image, int width, int height) {
        ViewGroup.LayoutParams layoutParams = image.getLayoutParams();
        layoutParams.height = height;
        layoutParams.width = width;
        image.setLayoutParams(layoutParams);

    }

    public class NewsType3ViewHolder extends RecyclerView.ViewHolder {
        protected TextView textTitle;
        protected LinearLayout newsType3LinyoutAD;
        protected ImageView authorIcon;
        protected TextView authorName;
        protected TextView newsTag;
        protected LinearLayout NewsType3LinyoutAuthor;
        protected TextView copyRightName;
        protected LinearLayout NewsType3LinyoutCopyRight;
        protected TextView readNewNum;
        protected LinearLayout newsType3LinyoutNoAD;
        protected LinearLayout newsType3;
        protected LinearLayout news_author_tag;
        protected LinearLayout pingbi;


        public NewsType3ViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View rootView) {
            newsType3 = (LinearLayout) rootView.findViewById(R.id.newsType3);
            news_author_tag = (LinearLayout) rootView.findViewById(R.id.news_author_tag);
            pingbi = (LinearLayout) rootView.findViewById(R.id.pingbi);
            textTitle = (TextView) rootView.findViewById(R.id.text_title);
            newsType3LinyoutAD = (LinearLayout) rootView.findViewById(R.id.newsType3_Linyout_AD);
            authorIcon = (ImageView) rootView.findViewById(R.id.author_icon);
            authorName = (TextView) rootView.findViewById(R.id.author_name);
            newsTag = (TextView) rootView.findViewById(R.id.news_tag);
            NewsType3LinyoutAuthor = (LinearLayout) rootView.findViewById(R.id.NewsType3_Linyout_author);
            copyRightName = (TextView) rootView.findViewById(R.id.copyRight_name);
            NewsType3LinyoutCopyRight = (LinearLayout) rootView.findViewById(R.id.NewsType3_Linyout_CopyRight);
            readNewNum = (TextView) rootView.findViewById(R.id.readNewNum);
            newsType3LinyoutNoAD = (LinearLayout) rootView.findViewById(R.id.newsType3_Linyout_NoAD);
        }
    }

    public class NewsType4ViewHolder extends RecyclerView.ViewHolder {

        protected TextView textTitle;
        protected ImageView newImageView;
        protected LinearLayout newsType4LinyoutAD;
        protected LinearLayout NewsType4LinyoutAuthor;
        protected LinearLayout NewsType4LinyoutCopyRight;
        protected LinearLayout newsType4LinyoutNoAD;
        protected LinearLayout newsType4;

        public NewsType4ViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View rootView) {
            textTitle = (TextView) rootView.findViewById(R.id.text_title);
            newImageView = (ImageView) rootView.findViewById(R.id.new_imageView);
            newsType4LinyoutAD = (LinearLayout) rootView.findViewById(R.id.newsType4_Linyout_AD);
            newsType4 = (LinearLayout) rootView.findViewById(R.id.newsType4);

        }
    }


}
